<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <!-- addPermission 的参数 第一个表示类型type（此处添加的是子项所以type=2） 第二项为添加项的pid即父项的id （第一级的id固定为0） -->
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <!-- 在表格中展示树形必须使用row-key属性 -->
      <el-table border :data="list" row-key="id" default-expand-all :rules="rules">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <!-- 当type为1时表示为树形结构的第一级 子项不用有添加项 -->
            <!-- addPermission 的参数 第一个表示类型type（此处添加的是子项所以type=2） 第二项为添加项的pid即父项的id -->
            <el-button v-if="scope.row.type===1" type="text" @click="addPermission(2,scope.row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="formData.id?'编辑权限':'添加权限'" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permisson'
import { transListtoTreeData } from '@/utils'
export default {
  data() {
    return {
      showDialog: false,
      list: [], // 获取到的权限数据
      formData: {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限数据
    async getPermissionList() {
      try {
        const res = await getPermissionList()
        // console.log(res)
        // 将扁平数据改造为树形结构数据
        this.list = transListtoTreeData(res, '0')
      // console.log(this.list)
      } catch (error) {
        console.log(error)
      }
    },
    async addPermission(type, pid) {
      try {
        this.formData.type = type
        this.formData.pid = pid
        this.showDialog = true
      } catch (error) {
        console.log(error)
      }
    },
    async editPermission(id) {
      try {
        this.formData.id = id
        this.showDialog = true
        const res = await getPermissionDetail(id)
        // console.log(res)
        this.formData = res
      } catch (error) {
        console.log(error)
      }
    },
    async delPermission(id) {
      try {
        // 二次确认
        await this.$confirm('是否删除？')
        await delPermission(id)
        this.$message.success('删除成功')
        // 刷新数据
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    async btnOK() {
      try {
        // 校验表单
        this.$refs.perForm.validate()

        if (this.formData.id) {
          // 编辑
          await updatePermission(this.formData)
          this.$message.success('编辑权限成功')
        } else {
          // 添加权限
          await addPermission(this.formData)
          this.$message.success('添加权限成功')
        }
        // 刷新数据
        this.getPermissionList()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 清空表单
      this.formData = {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        id: ''
      }
      // 2. 清空校验数据
      this.$refs.perForm.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
